﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EEG_NIRS_Record
{
    class MyPictureBox : PictureBox
    {
        List<string> label_list;
        Rectangle rc;
        int count_of_ch = 0;
        Bitmap image;
        Graphics g;
        Pen black_pen;
        Pen blue_pen;
        SolidBrush white_brush;
        int x;
        private ComboBox comboBox1;
        int[] old_data;
        //public event EventHandler Resize;  // 引发Resize事件

        public MyPictureBox()
        {
            label_list = new List<string>();

            rc = new Rectangle();

            //image = new Bitmap(this.Width, this.Height);
            //this.Image = image;
            //g = Graphics.FromImage(image);            

            black_pen = new Pen(Color.Black, 2);
            blue_pen = new Pen(Color.Blue, 2);
            white_brush = new SolidBrush(Color.White);

            x = 0;

        }
        public void Init()
        {
            if (image != null)
            {
                image.Dispose();
            }
            if (g != null)
            {
                g.Dispose();
            }
            image = new Bitmap(this.Width, this.Height);
            this.Image = image;
            g = Graphics.FromImage(image); 
        }
        public void SetLabel(List<string> list)
        {
            label_list = list.ToList();
            count_of_ch = list.Count;
            old_data = new int[count_of_ch];
        }
        public void AddData(int data_len)  // int[,] data
        {
            //int data_len = data.GetLength(0);

            if (x >= this.Width)
            {
                x = 0;
            }
            if (x == 0)
            {
                g.FillRectangle(white_brush, x, 0, 2, this.Height);
            }
            g.FillRectangle(white_brush, x + 1, 0, data_len, this.Height);
            for (int i = 0; i < data_len; i++)
            {
                for (int j = 0; j < 64; j++)
                {
                    //data[i, j] = 0;
                    g.DrawLine(blue_pen, x, this.Height * (j + 1) / (64 + 1) - 0, x + 1, this.Height * (j + 1) / (64 + 1) - 0);
                    //old_data[j] = 0;
                }
                x++;
            }
            g.DrawLine(black_pen, x + 0 + data_len, 0, x + 0 + data_len, this.Height);

            rc.Y = 0;
            rc.X = x - data_len;
            rc.Width = data_len;
            rc.Height = this.Height;
        }
        public void UpdateImage()
        {
            this.Invalidate(rc);
        }
        protected override void OnResize(EventArgs e)
        {            
            //EventHandler handler = Resize;
            //if (handler != null)
            //{
            //    handler(this, e);
            //}
            Init();
        }

        private void InitializeComponent()
        {
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            // 
            // comboBox1
            // 
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(0, 0);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(121, 20);
            this.comboBox1.TabIndex = 0;
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);

        }
    }
}
